package com.joolun.web.controller.mall;

import cn.binarywang.wx.miniapp.bean.shortlink.GenerateShortLinkRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.common.enums.validate.Add;
import com.joolun.common.enums.validate.Query;
import com.joolun.common.utils.SecurityUtils;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.mall.domain.dto.ChainOrderDetailDTO;
import com.joolun.mall.domain.vo.ChainOrderDetailVO;
import com.joolun.mall.entity.Chain;
import com.joolun.mall.entity.ChainPlan;
import com.joolun.mall.service.IChainDetailService;
import com.joolun.mall.service.IChainOrderService;
import com.joolun.mall.service.IChainPlanService;
import com.joolun.mall.service.IChainService;
import com.joolun.mall.util.WxUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 管理端-接龙管理
 *
 * @author www.joolun.com
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/chainPlan")
public class ChainPlanController extends BaseController
{
    @Autowired
    private IChainPlanService planService;

    @Autowired
    private IChainService chainService;

    @Autowired
    private IChainDetailService chainDetailService;

    @Autowired
    private IChainOrderService orderService;

    /**
     * 接龙计划--详情
     * @param planId 接龙计划id
     * @return AjaxResult
     */
    @Log(title = "接龙计划详情", businessType = BusinessType.QUERY)
    @ApiOperation(value = "接龙计划详情")
    @GetMapping("detail/{planId}")
//    @PreAuthorize("@ss.hasPermi('mall:goodsspu:get')")
    public AjaxResult<ChainPlan> getById(@PathVariable("planId") Long planId){
        ChainPlan chainPlan = planService.detail(planId);

        return AjaxResult.success(planService.detail(planId));
    }
    /**
     * 接龙计划-分页查询
     */
    @GetMapping("/page")
    @Log(title = "接龙计划列表", businessType = BusinessType.QUERY)
    public AjaxResult<Page<ChainPlan>> page(Page<ChainPlan> page, ChainPlan dto)
    {
        return AjaxResult.success(planService.page1(page, dto));
    }


    /**
     * 接龙计划-新增
     * @param dto
     * @return
     */
    @Log(title = "接龙计划-新增", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ChainPlan dto)
    {
        return AjaxResult.success(planService.add(dto));
    }


    /**
     * 接龙计划-修改
     * @param dto 接龙计划
     * @return
     */
    @Log(title = "接龙计划-修改", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ChainPlan dto)
    {
        dto.setUpdateBy(SecurityUtils.getUsername());
        dto.setUpdateTime(new Date());
        return AjaxResult.success(planService.updateById(dto));
    }

    /**
     * 接龙计划-获取微信群发消息
     * @param planId 计划id
     * @return
     */
    @Log(title = "接龙计划-获取微信群发消息", businessType = BusinessType.QUERY)
    @GetMapping("getWxMsg/{planId}")
    public AjaxResult<String> getWxMessage(@PathVariable("planId") Long planId)
    {
        return AjaxResult.success(planService.getWxMsg(planId));
    }


    //-----------------------------------------------------接龙链接-----------------------------------------------------

    /**
     * 接龙链接-详情
     * @param chainId 接龙id
     * @return AjaxResult
     */
    @ApiOperation(value = "接龙详情")
    @Log(title = "接龙详情", businessType = BusinessType.QUERY)
    @GetMapping("chain/detail/{chainId}")
    public AjaxResult<Chain> chainDetail(@PathVariable("chainId") Long chainId){
        return AjaxResult.success(chainService.detail(chainId,false));
    }

    /**
     * 接龙链接--分页查询
     * @param page
     * @param dto
     * @return
     */
    @Log(title = "接龙列表", businessType = BusinessType.QUERY)
    @GetMapping("/chain/page")
    public AjaxResult<Page<Chain>> page(Page<Chain> page, Chain dto)
    {
        return AjaxResult.success(chainService.page1(page, dto));
    }

    /**
     * 接龙链接-新增
     * @param dto
     * @return
     */
    @Log(title = "接龙链接-新增", businessType = BusinessType.INSERT)
    @PostMapping("addLink")
    public AjaxResult addLink(@RequestBody @Validated(Add.class) Chain dto)
    {
        chainService.addLinks(Arrays.asList(dto));
        return AjaxResult.success();
    }

    /**
     * 接龙链接-批量新增
     * @param dto
     * @return
     */
    @Log(title = "接龙链接-新增", businessType = BusinessType.INSERT)
    @PostMapping("addLinks")
    public AjaxResult addLinks(@RequestBody @Valid @Validated({Add.class}) List<Chain> dto)
    {
        chainService.addLinks(dto);
        return AjaxResult.success();
    }


    /**
     * 接龙链接-修改
     * @param dto
     * @return
     */
    @Log(title = "接龙链接-修改", businessType = BusinessType.UPDATE)
    @PutMapping("updateLink")
    public AjaxResult updateLink(@RequestBody Chain dto)
    {
        chainService.updateLink(dto);
        return AjaxResult.success();
    }

    /**
     * 接龙链接-批量删除
     * @param chainIds 链接ids
     * @return
     */
    @Log(title = "接龙链接-批量删除", businessType = BusinessType.DELETE)
    @PutMapping("deleteLink")
    public AjaxResult deleteLink(@RequestBody List<Long> chainIds)
    {
        chainService.deleteLink(chainIds);
        return AjaxResult.success();
    }

//    /**
//     * 接龙链接-批量删除
//     * @param chain 传id导出接龙id信息 传planId导出接龙计划里面所有的接龙链接
//     * @return
//     */
//    @PutMapping("exportOrders")
//    public AjaxResult exportOrders(@RequestBody Chain chain)
//    {
////        chainService.exportOrders(chain);
//        return AjaxResult.success();
//    }

    /**
     * 接龙链接-删除接龙链接SKU
     * @param chainDetailIds 接龙链接明细ids
     * @return
     */
    @Log(title = "接龙链接-删除接龙链接SKU", businessType = BusinessType.DELETE)
    @PutMapping("deleteLinkSku")
    public AjaxResult deleteLinkSku(@RequestBody List<Long> chainDetailIds)
    {
        chainDetailService.deleteDetails(chainDetailIds);
        return AjaxResult.success();
    }


//    /**
//     * 删除接龙链接
//     * @param dto
//     * @return
//     */
//    @PutMapping("deleteLinkSku")
//    public AjaxResult deleteLinkSku(Chain dto)
//    {
//        return AjaxResult.success();
//    }

    //-----------------------------------------------------接龙订单信息导出-----------------------------------------------------

    /**
     * 接龙订单-导出订单
     * @param dto 接龙对象 如果传planId  则导出该接龙计划下的所有订单 如果传chainIds 多个链接id 则导出链接下的所有订单
     * @return
     */
    @Log(title = "接龙订单-导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportOrders")
    public AjaxResult exportOrders(@RequestBody ChainOrderDetailDTO dto)
    {
        List<ChainOrderDetailVO> list = orderService.selectOrderDetails(dto);
        ExcelUtil<ChainOrderDetailVO> util = new ExcelUtil<>(ChainOrderDetailVO.class);
        return util.exportExcel(list, "接龙订单信息");
    }

    /**
     * 接龙订单-导出核对明细
     * @param dto 接龙对象 如果传planId  则导出该接龙计划下的所有订单 如果传chainIds 多个链接id 则导出链接下的所有订单
     * @return
     */
    @Log(title = "接龙订单-导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportCheckOrders")
    public AjaxResult exportCheckOrders(@RequestBody ChainOrderDetailDTO dto)
    {
        return new AjaxResult(200,"操作成功",orderService.selectCheckOrderDetails(dto));
    }

    /**
     *  生成接龙链接
     * @param dto
     * @return
     */
    @Log(title = "生成接龙链接", businessType = BusinessType.QUERY)
    @PostMapping("/genLink")
    public AjaxResult genLink(@RequestBody @Validated(Query.class) Chain dto)
    {
        return new AjaxResult(200,"操作成功",chainService.genLink(dto));
    }

    @Log(title = "接龙订单-生成合集列表短连接", businessType = BusinessType.OTHER)
    @PostMapping("/genLinkList")
    public AjaxResult genLinkList(@RequestBody @Validated(Query.class) ChainPlan dto)
    {
        GenerateShortLinkRequest shortLinkRequest = GenerateShortLinkRequest.builder()
                .pageUrl("pages/goods/linkList?id=" + dto.getId())
//                .pageUrl("pages/goods/linkDetail")
//                .pageTitle(chain.getName())
                .isPermanent(false)
                .build();
        return new AjaxResult(200,"操作成功", WxUtil.genLink(shortLinkRequest));
    }

    @Log(title = "接龙-查找sku信息", businessType = BusinessType.OTHER)
    @GetMapping("/findSkuInfo")
    public AjaxResult findSkuInfo(@RequestParam("planId") Long planId,@RequestParam("skuCode") String skuCode)
    {
        return new AjaxResult(200,"操作成功", chainService.detail(planId,skuCode));
    }
}
